import {
  onBeforeUnmount
} from "vue";
import {
  onBeforeRouteLeave
} from "vue-router";

export default function useGetMessageCaptcha() {
  let timer;

  const getMessageCaptcha = (codeButton, countType) => {
    // 验证码倒计时

    let count = localStorage.getItem(countType) || 60;

    timer = setInterval(() => {
      count--;
      localStorage.setItem(countType, count); // 每次减少时更新 localStorage

      if (count === 0) {
        clearInterval(timer);
        localStorage.removeItem(countType); // 倒计时结束后清除存储
        codeButton.value.disabled = false;
        codeButton.value.innerText = '获取验证码';
      } else {

        if (codeButton.value !== null) {
          codeButton.value.disabled = true;
          codeButton.value.innerText = `${count}秒后重发`;
        }
      }
    }, 1000);


  }


  return {
    getMessageCaptcha
  }

}